/**
 * Created by yy on 2017/7/25.
 */
/***************************************
 由于Chrome、IOS10等已不再支持非安全域的浏览器定位请求，为保证定位成功率和精度，请尽快升级您的站点到HTTPS。
 ***************************************/

var map, geolocation;
//加载地图，调用浏览器定位服务
var getLonAndLat = '';
map = new AMap.Map('container', {
    resizeEnable: true
});



var $state1 = $('#state1');
var $state2 = $('#state2');
var $html1 = '未签到';
var $html2 = '未签退';

var url = location.search;//获取兼职信息id
if (url.indexOf("?") != -1) {
    var messageid = url.substr(4);
}

map.plugin('AMap.Geolocation', function() {
    geolocation = new AMap.Geolocation({
        enableHighAccuracy: true,//是否使用高精度定位，默认:true
        timeout: 10000,          //超过10秒后停止定位，默认：无穷大
        buttonOffset: new AMap.Pixel(10, 0),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
        zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
        buttonPosition:'RB'
    });

    map.addControl(geolocation);
    geolocation.getCurrentPosition();
    AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
    AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
});
//解析定位结果



function onComplete(data) {
    var str=['定位成功'];
    str.push('经度：' + data.position.getLng());
    str.push('纬度：' + data.position.getLat());
    getLonAndLat = data.position.getLng()+","+ data.position.getLat();//构造经纬度
    sessionStorage.getLonAndLat = getLonAndLat;
    setState();
    LonAndLatToAddress(getLonAndLat);//返回经纬度地址

    if(data.accuracy){

        str.push('精度：' + data.accuracy + ' 米');


    }//如为IP精确定位结果则没有精度信息
    str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
    //document.getElementById('tip').innerHTML = str.join('<br>');
}



function  LonAndLatToAddress(getLonAndLat){
    AMap.plugin('AMap.Geocoder',function(){
        var geocoder = new AMap.Geocoder({
            city: "021"//城市，默认：“上海”
        });
        var address1;
        geocoder.getAddress(getLonAndLat,function(status,result){
            if(status=='complete'){
                address1= result.regeocode.formattedAddress;

                sessionStorage.address2=address1;//储存用户定位地址

                //alert(address1);
                $('#address').html(address1);
            }

        });

    });
}

//解析定位错误信息
function onError(data) {
    document.getElementById('tip').innerHTML = '定位失败';
}




function setState(){
    var url1 = server + '/server/SignInAndSignOut/signInMessage.json';
    var json1 = {"workMessageId": messageid, "usersLonAndLat": getLonAndLat};
    $.post(url1, json1, function (data) {
        if (data.code == 12000) {
            alert("登录超时");
            window.location.replace('../../login.html');
        }

        if(data.code == 11001|| data.code == 13102){

        }
        else {
            var state = data.message;


            var state1 = data.data.status.substring(0, 2);
            var state2 = "未签退";
            if (data.data.status.length > 3) {
                state2 = data.data.status.substr(3);
            }
            switch (state) {
                case "未到签到时间":
                    $html1 = '时间未到';
                    $html2 = state2;
                    $('#btn').attr('disabled', true);
                    break;
                case "未到指定签到范围":
                    $html1 = '不在范围';
                    $html2 = state2;
                    $('#btn').attr('disabled', true);
                    break;
                case "未到指定签退范围":
                    $html1 = state1;
                    $html2 = '不在范围';
                    $('#btn').attr('disabled', true);
                    break;
                case "可以打卡":
                    $html1 = '可签到';
                    $html2 = state2;
                    $('#btn').attr('disabled', false);
                    break;
                case "可以签退":
                    $html1 = state1;
                    $html2 = '可签退';
                    $('#btn').attr('disabled', false);
                    break;
                default:
                    $html1 = state1;
                    $html2 = state2;
                    $('#btn').attr('disabled', true);
                    break;
            }
        }
        $('#state1').html($html1);
        $('#state2').html($html2);
    });
}


